package com.atguigu.Josepfu;

/**
 * description:
 * Created by 才睡醒 on 2021-06-09
 */
public class Josefu {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
      //  circleSingleLinkedList.boyList();
        circleSingleLinkedList.countBoy(1,2,5);
    }
}

//创建一个环形的单项链表
class CircleSingleLinkedList {
    //创建一个first节点，当前没有编号
    private Boy first = null;

    //添加小孩节点，构建一个环形的链表
    public void addBoy(int num) {

        if (num < 1) {
            System.out.println("输入错误");
            return;
        }
        Boy curBoy = null;//辅助指针，帮忙构建环形链表
        //新建一个小孩对象
        for (int i = 1; i <= num; i++) {
            Boy boy = new Boy(i);
            if (i == 1) {
                first = boy;//first指向第一个小孩
                first.setNext(first);//第一个小孩的next指向first
                curBoy = first; //让curBoy指向第一个小孩
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }

        }

    }

    public void boyList() {
        //判断链表是否为空
        if (first == null) {
            System.out.println("链表为空");
            return;
        }
        //因为first不能移动，所以建立一个辅助变量
        Boy curBoy = first;
        while (true) {
            System.out.printf("小孩的编号%d \n", curBoy.getNo());
            if (curBoy.getNext() == first) {//说明已经遍历完毕
                break;
            }
            curBoy = curBoy.getNext(); //curBoy后移
        }
    }


    //根据用户的输入，计算出小孩出圈的顺序
    public void countBoy(int startNo, int countNum, int nums) {
        if (first == null || startNo < 1 || startNo > nums) {
            System.out.println("输入错误");
            return;
        }
        //创建一个辅助指针，帮助完成小孩出圈
        Boy helper = first;

        while (true) {

            if (helper.getNext() == first) {//如果helper的next等于first，表示找到最后一个元素
                break;
            }
            helper = helper.getNext();  //helper后移
        }
        //小孩报数前，先让first和helper移动k-1次
        for (int i = 0; i < startNo-1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //当小孩报数时，让first和helper指针同时的移动m-1次，然后出圈。
        //这里是一个循环操作，直到圈中只有一个节点
        while (true){
            if(helper==first){//说明圈中只有一个节点
                break;
            }
            //让first和helper指针同时的移动countNum -1
            for (int i = 0; i <countNum-1 ; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println("出圈小孩"+first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.println("最后留在圈内的小孩"+first.getNo());
    }

}

//创建一个Boy类，表示一个节点
class Boy {
    private int no;//编号
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }


}
